package com.rocksea.app.db.HighStrain;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;

import android.database.sqlite.SQLiteDatabase;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.SQLiteHelper;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;
import com.rocksea.app.db.PileId;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.db.lowStrain.LowStrainDb;
import com.rocksea.app.entry.HighStarinDb;

import java.util.ArrayList;

/**
 * Created by 吴志华 on 2017/7/28.
 *
 */

public class GyDbHelper implements SQLiteHelper.OnUpdateListener
{
    private static LiteOrm mGyDb;
    private static GyDbHelper mGyDbHelper;
    private GyDbHelper(Context context) {
        //初始化数据库
        DataBaseConfig config = new DataBaseConfig(context);
        config.dbName = Environment.getExternalStorageDirectory().getPath()+"/RailDatabase/db/GyBasicInfo.db";
        config.dbVersion = 1;
        mGyDb = LiteOrm.newCascadeInstance(config);
        mGyDb.setDebugged(true);
        HighStrain highStrain = new HighStrain();
        mGyDb.save(highStrain);
        mGyDb.delete(highStrain);
    }
    public static GyDbHelper getInstance(Context context)
    {
        if (mGyDb==null){
            synchronized (GyDbHelper.class){
                mGyDbHelper =new GyDbHelper(context);
            }
        }

        return mGyDbHelper;
    }

    @Override
    public void onUpdate(SQLiteDatabase sqLiteDatabase, int i, int i1)
    {
        String sql = "Alter table "+"GyBasicInfo"+" add column "+"inspectionFormId"+" TEXT ";
        sqLiteDatabase.execSQL(sql);

        sql = "Alter table "+"GyBasicInfo"+" add column "+"pileId"+" TEXT ";
        sqLiteDatabase.execSQL(sql);

        sql = "Alter table "+"GyBasicInfo"+" add column "+"inspectionFormNum"+" TEXT ";
        sqLiteDatabase.execSQL(sql);
    }

    public int delete(String baseInfoId) {
        WhereBuilder whereBuilder = new WhereBuilder(HistoryTable.class);
        whereBuilder.where("BaseInfoId LIKE ?",baseInfoId);
        return mGyDb.delete(whereBuilder);
    }

    public HighStrain queryByBaseInfoId(String baseInfoId)
    {
        QueryBuilder<HighStrain> queryBuilder = new QueryBuilder<>(HighStrain.class);
        WhereBuilder whereBuilder = new WhereBuilder(HistoryTable.class);
        whereBuilder.where("uuid LIKE ?",baseInfoId);
        queryBuilder.where(whereBuilder);
        ArrayList<HighStrain> highStrains = mGyDb.query(queryBuilder);
        if (highStrains.size()>0)
        {
            return highStrains.get(0);
        }
        else
            return null;
    }

    public String queryJSON(String baseInfoId)
    {
        String sql = "SELECT sourceData FROM HighStrain WHERE uuid = ?";
        Cursor cursor = mGyDb.getWritableDatabase().rawQuery(sql,new String[]{baseInfoId});
        if (cursor.moveToFirst())
        {
            String json = cursor.getString(cursor.getColumnIndex("sourceData"));
            cursor.close();
            return json;
        }
        cursor.close();
        return null;
    }

    public PileId queryPileId(String baseInfoId)
    {
        String sql = "SELECT pileId,inspectionFormId,inspectionFormNum FROM HighStrain WHERE uuid = ?";
        Cursor cursor = mGyDb.getWritableDatabase().rawQuery(sql,new String[]{baseInfoId});
        if (cursor.moveToFirst())
        {
            int pileId = cursor.getInt(cursor.getColumnIndex("pileId"));
            String inspectionFormNum = cursor.getString(cursor.getColumnIndex("inspectionFormNum"));
            long inspectionFormId = cursor.getLong(cursor.getColumnIndex("inspectionFormId"));
            cursor.close();
            PileId pileInfo = new PileId();
            pileInfo.pileId = pileId;
            pileInfo.planId = inspectionFormId;
            pileInfo.planNum = inspectionFormNum;
            return pileInfo;
        }
        cursor.close();
        return null;
    }

    public void makeAsUpLoad(String baseInfoId)
    {
        WhereBuilder whereBuilder = new WhereBuilder(HighStrain.class);
        whereBuilder.andEquals("uuid",baseInfoId);
        ColumnsValue columnsValue = new ColumnsValue(new String[]{"isLoad"},new Object[]{1});
        mGyDb.update(whereBuilder,columnsValue, ConflictAlgorithm.Fail);
    }




    public void save(HighStrain highStrain)
    {
        String sql = "SELECT id FROM HighStrain WHERE uuid = ?";
        Cursor cursor = mGyDb.getWritableDatabase().rawQuery(sql,new String[]{highStrain.uuid});
        if (!cursor.moveToFirst())
        {
            mGyDb.insert(highStrain);
        }
        cursor.close();
    }
}
